﻿using Kaskelot.SP.Core;
using Kaskelot.SP.Services;
using Microsoft.SharePoint.Publishing.Navigation;
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace Kaskelot.SP.UI.CONTROLTEMPLATES.Kaskelot.SP.UI
{
	public partial class Navigation : UserControl
	{
        protected void Page_Load(object sender, EventArgs e)
        {
            Logger.LogVerbose(this, LayerNames.UI, "Page_Load", "Entered -> in Navigation");
            try
            {
                NavigationService svc = new NavigationService();

                this.repTopNavigation.DataSource = svc.GetTopNavigationNodes(PortalSiteMapProvider.CombinedNavSiteMapProvider.RootNode);
                this.repTopNavigation.DataBind();
            }
            catch (Exception ex)
            {
                Logger.LogError(this, LayerNames.UI, "Page_Load", "Error occured", ex);
            }
            Logger.LogVerbose(this, LayerNames.UI, "Page_Load", "Exiting");
        }

        protected string GetActive(SiteMapNode node)
        {
            SiteMapNode current = PortalSiteMapProvider.CombinedNavSiteMapProvider.CurrentNode;
            SiteMapNode root = PortalSiteMapProvider.CombinedNavSiteMapProvider.RootNode;

            return (current == node) || (current.IsDescendantOf(node) && node != root) ? "nav-active" : string.Empty;
        }
	}
}
